// pages/shopManager/goodsManager/goodsEdit/goodsEdit.js
const common = require('../../../../utils/util.js')
const md5 = require('../../../../utils/md5.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsId: null,
        goodsType: null,
        goodsInfo: null,
        goodsBannerImg: '',
        goodsMainImg: '',
        goodsName: '',
        goodsBrief: '',
        goodsFormat: [{
            color: '',
            img: '',
            price: 0,
            retail_price: 0,
            factory_price: 0,
            extra_money: 0,
            sku_num: 0
        }],
        goodsSubImg: [],
        goodsDetailImg: [],
        goodsSort: 0,
        goodsPoint: 0,
        goodsShare: 999,
        goodsPointChange: 999,
        onSale: true,
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.goodsId) {
            this.setData({
                goodsId: options.goodsId,
                goodsType: options.goodsType
            })
            this.getGoodsInfo()
        } else {
            this.setData({
                goodsType: options.goodsType
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        let pages = getCurrentPages(); //获取当前页面实例
        let prepage = pages[pages.length - 2]
        if (prepage == undefined || prepage == null) return;
        prepage.updateData()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getGoodsInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            goods_id: this.data.goodsId,
            paramter: 1
        }
        common.post('Goods/goods_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            if (that.data.goodsType == 'normal') {
                that.setData({
                    goodsMainImg: {
                        major_img_big: res[0].image,
                        major_img_middle: res[0].image_middle,
                        major_img_thumb: res[0].image_thumb
                    },
                    goodsName: res[0].goods_name,
                    goodsBrief: res[0].brief,
                    goodsFormat: res[0].sku,
                    goodsSubImg: res[0].vice_big,
                    goodsDetailImg: res[0].info,
                    goodsSort: res[0].sort,
                    goodsPoint: res[0].project_money,
                    onSale: res[0].is_sale == 1 ? true : false
                })
            }
            else if (that.data.goodsType == 'new'){
                that.setData({
                    goodsMainImg: {
                        major_img_big: res[0].image,
                        major_img_middle: res[0].image_middle,
                        major_img_thumb: res[0].image_thumb
                    },
                    goodsName: res[0].goods_name,
                    goodsBrief: res[0].brief,
                    goodsFormat: res[0].sku,
                    goodsSubImg: res[0].vice_big,
                    goodsDetailImg: res[0].info,
                    goodsSort: res[0].sort,
                    onSale: res[0].is_sale == 1 ? true : false
                })
            }
            else if (that.data.goodsType == 'share') {
                that.setData({
                    goodsMainImg: {
                        major_img_big: res[0].image,
                        major_img_middle: res[0].image_middle,
                        major_img_thumb: res[0].image_thumb
                    },
                    goodsName: res[0].goods_name,
                    goodsBrief: res[0].brief,
                    goodsFormat: res[0].sku,
                    goodsSubImg: res[0].vice_big,
                    goodsDetailImg: res[0].info,
                    goodsSort: res[0].sort,
                    goodsShare: res[0].share_mun,
                    onSale: res[0].is_sale == 1 ? true : false
                })
            }
            else if (that.data.goodsType == 'point') {
                that.setData({
                    goodsMainImg: {
                        major_img_big: res[0].image,
                        major_img_middle: res[0].image_middle,
                        major_img_thumb: res[0].image_thumb
                    },
                    goodsName: res[0].goods_name,
                    goodsBrief: res[0].brief,
                    goodsFormat: res[0].sku,
                    goodsSubImg: res[0].vice_big,
                    goodsDetailImg: res[0].info,
                    goodsSort: res[0].sort,
                    goodsPointChange: res[0].need_project_money,
                    onSale: res[0].is_sale == 1 ? true : false
                })
            }
        }).catch(function(e) {})
    },

    goodsNameInput: function(e) {
        this.setData({
            goodsName: e.detail.value
        })
    },

    goodsBriefInput: function(e) {
        this.setData({
            goodsBrief: e.detail.value
        })
    },

    myCatchTouch: function() {
        return
    },

    formatNameInput: function(e) {
        let goodsFormat = this.data.goodsFormat
        goodsFormat[e.currentTarget.dataset.index].color = e.detail.value
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    formatPriceInput: function(e) {
        let goodsFormat = this.data.goodsFormat
        goodsFormat[e.currentTarget.dataset.index].price = e.detail.value
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    formatPriceInputBlur: function(e) {
        if (e.detail.value == '') {
            let goodsFormat = this.data.goodsFormat
            goodsFormat[e.currentTarget.dataset.index].price = 0
            this.setData({
                goodsFormat: goodsFormat
            })
        }
    },

    formatRetailPriceInput: function(e) {
        let goodsFormat = this.data.goodsFormat
        goodsFormat[e.currentTarget.dataset.index].retail_price = e.detail.value
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    formatRetailPriceBlur: function(e) {
        if (e.detail.value == '') {
            let goodsFormat = this.data.goodsFormat
            goodsFormat[e.currentTarget.dataset.index].retail_price = 0
            this.setData({
                goodsFormat: goodsFormat
            })
        }
    },

    formatFactoryPriceInput: function(e) {
        let goodsFormat = this.data.goodsFormat
        goodsFormat[e.currentTarget.dataset.index].factory_price = e.detail.value
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    formatFactoryPriceBlur: function(e) {
        if (e.detail.value == '') {
            let goodsFormat = this.data.goodsFormat
            goodsFormat[e.currentTarget.dataset.index].factory_price = 0
            this.setData({
                goodsFormat: goodsFormat
            })
        }
    },

    formatExtraPriceInput: function(e) {
        let goodsFormat = this.data.goodsFormat
        goodsFormat[e.currentTarget.dataset.index].extra_money = e.detail.value
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    formatExtraPriceBlur: function(e) {
        if (e.detail.value == '') {
            let goodsFormat = this.data.goodsFormat
            goodsFormat[e.currentTarget.dataset.index].extra_money = 0
            this.setData({
                goodsFormat: goodsFormat
            })
        }
    },

    formatStockInput: function(e) {
        let goodsFormat = this.data.goodsFormat
        goodsFormat[e.currentTarget.dataset.index].sku_num = e.detail.value
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    formatStockBlur: function(e) {
        if (e.detail.value == '') {
            let goodsFormat = this.data.goodsFormat
            goodsFormat[e.currentTarget.dataset.index].sku_num = 0
            this.setData({
                goodsFormat: goodsFormat
            })
        }
    },

    addFormat: function() {
        let goodsFormat = this.data.goodsFormat
        let formatData = {
            color: '',
            img: '',
            price: 0,
            retail_price: 0,
            factory_price: 0,
            extra_money: 0,
            sku_num: 0
        }
        goodsFormat.push(formatData)
        this.setData({
            goodsFormat: goodsFormat
        })
    },

    deleteFormat: function(e) {
        const that = this
        let goodsFormat = this.data.goodsFormat
        if (this.data.goodsId == null) {
            goodsFormat.splice(e.currentTarget.dataset.index, 1)
            this.setData({
                goodsFormat: goodsFormat
            })
        } else {
            wx.showLoading({
                title: '请稍后'
            })
            let data = {
                sku_id: e.currentTarget.dataset.id
            }
            common.post('Goods/del_sku', data).then(function(res) {
                wx.hideLoading()
                if (res.code == 204) {
                    wx.showModal({
                        title: '提示',
                        content: res.error,
                        showCancel: false,
                        success(res) {}
                    })
                    return
                }
                wx.showModal({
                    title: '提示',
                    content: '删除规格成功',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            goodsFormat.splice(e.currentTarget.dataset.index, 1)
                            that.setData({
                                goodsFormat: goodsFormat
                            })
                        }
                    }
                })
            })
        }
    },

    deleteSubImg: function(e) {
        const that = this
        let goodsSubImg = this.data.goodsSubImg
        if (this.data.goodsId == null) {
            goodsSubImg.splice(e.currentTarget.dataset.index, 1)
            this.setData({
                goodsSubImg: goodsSubImg
            })
        } else {
            wx.showLoading({
                title: '请稍后'
            })
            let data = {
                image_id: e.currentTarget.dataset.id
            }
            common.post('Goods/del_goods_img', data).then(function(res) {
                wx.hideLoading()
                if (res.code == 204) {
                    wx.showModal({
                        title: '提示',
                        content: res.error,
                        showCancel: false,
                        success(res) {}
                    })
                    return
                }
                wx.showModal({
                    title: '提示',
                    content: '删除图片成功',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            goodsSubImg.splice(e.currentTarget.dataset.index, 1)
                            that.setData({
                                goodsSubImg: goodsSubImg
                            })
                        }
                    }
                })
            })
        }
    },

    deleteDetailImg: function(e) {
        const that = this
        let goodsDetailImg = this.data.goodsDetailImg
        if (this.data.goodsId == null) {
            goodsDetailImg.splice(e.currentTarget.dataset.index, 1)
            this.setData({
                goodsDetailImg: goodsDetailImg
            })
        } else {
            wx.showLoading({
                title: '请稍后'
            })
            let data = {
                image_id: e.currentTarget.dataset.id
            }
            common.post('Goods/del_goods_img', data).then(function(res) {
                wx.hideLoading()
                if (res.code == 204) {
                    wx.showModal({
                        title: '提示',
                        content: res.error,
                        showCancel: false,
                        success(res) {}
                    })
                    return
                }
                wx.showModal({
                    title: '提示',
                    content: '删除图片成功',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            goodsDetailImg.splice(e.currentTarget.dataset.index, 1)
                            that.setData({
                                goodsDetailImg: goodsDetailImg
                            })
                        }
                    }
                })
            })
        }
    },

    goodsPointInput: function(e) {
        this.setData({
            goodsPoint: e.detail.value
        })
    },

    goodsPointBlur: function(e) {
        if (e.detail.value == '' || e.detail.value == null) {
            this.setData({
                goodsPoint: 0
            })
        }
    },

    goodsShareInput: function (e) {
        this.setData({
            goodsShare: e.detail.value
        })
    },

    goodsShareBlur: function (e) {
        if (e.detail.value == '' || e.detail.value == null) {
            this.setData({
                goodsShare: 999
            })
        }
    },

    goodsPointChangeInput: function (e) {
        this.setData({
            goodsPointChange: e.detail.value
        })
    },

    goodsPointChangeBlur: function (e) {
        if (e.detail.value == '' || e.detail.value == null) {
            this.setData({
                goodsPointChange: 999
            })
        }
    },

    goodsSortInput: function(e) {
        this.setData({
            goodsSort: e.detail.value
        })
    },

    goodsSortBlur: function(e) {
        if (e.detail.value == '' || e.detail.value == null) {
            this.setData({
                goodsSort: 0
            })
        }
    },

    goSave: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.goodsType == 'agent' && this.data.goodsBannerImg == '') {
            wx.showToast({
                title: '请添加商品Banner图',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.goodsMainImg == '') {
            wx.showToast({
                title: '请添加商品主图',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.goodsName == '') {
            wx.showToast({
                title: '请填写商品标题',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.goodsFormat.find((e) => {
                if (e.color == '' || e.img == '') {
                    return true
                }
            })) {
            wx.showToast({
                title: '请完善商品规格信息',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.goodsSubImg.length == 0) {
            wx.showToast({
                title: '请添加商品副图',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.goodsDetailImg.length == 0) {
            wx.showToast({
                title: '请添加商品详情图',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '保存中',
        })
        const that = this
        let data
        if (this.data.goodsId == null) {
            if (this.data.goodsType == 'normal') {
                data = {
                    store_id: '1',
                    goods_name: this.data.goodsName,
                    brief: this.data.goodsBrief,
                    client: '1',
                    is_goods: '0',
                    class_id: '1',
                    image: this.data.goodsMainImg.major_img_big,
                    image_middle: this.data.goodsMainImg.major_img_middle,
                    image_thumb: this.data.goodsMainImg.major_img_thumb,
                    viceImages: JSON.stringify(this.data.goodsSubImg),
                    infoImages: JSON.stringify(this.data.goodsDetailImg),
                    sku_list: JSON.stringify(this.data.goodsFormat),
                    sort: this.data.goodsSort,
                    project_money: this.data.goodsPoint,
                    is_sale: this.data.onSale ? '1' : '0'
                }
            }
            else if (this.data.goodsType == 'new'){
                data = {
                    store_id: '1',
                    goods_name: this.data.goodsName,
                    brief: this.data.goodsBrief,
                    client: '1',
                    is_goods: '11',
                    new_goods: '1',
                    class_id: '1',
                    image: this.data.goodsMainImg.major_img_big,
                    image_middle: this.data.goodsMainImg.major_img_middle,
                    image_thumb: this.data.goodsMainImg.major_img_thumb,
                    viceImages: JSON.stringify(this.data.goodsSubImg),
                    infoImages: JSON.stringify(this.data.goodsDetailImg),
                    sku_list: JSON.stringify(this.data.goodsFormat),
                    sort: this.data.goodsSort,
                    is_sale: this.data.onSale ? '1' : '0'
                }
            }
            else if (this.data.goodsType == 'share') {
                data = {
                    store_id: '1',
                    goods_name: this.data.goodsName,
                    brief: this.data.goodsBrief,
                    client: '1',
                    is_goods: '11',
                    share_goods: '1',
                    class_id: '1',
                    image: this.data.goodsMainImg.major_img_big,
                    image_middle: this.data.goodsMainImg.major_img_middle,
                    image_thumb: this.data.goodsMainImg.major_img_thumb,
                    viceImages: JSON.stringify(this.data.goodsSubImg),
                    infoImages: JSON.stringify(this.data.goodsDetailImg),
                    sku_list: JSON.stringify(this.data.goodsFormat),
                    sort: this.data.goodsSort,
                    share_mun: this.data.goodsShare,
                    is_sale: this.data.onSale ? '1' : '0'
                }
            }
            else if (this.data.goodsType == 'point') {
                data = {
                    store_id: wx.getStorageSync('storeId'),
                    goods_name: this.data.goodsName,
                    brief: this.data.goodsBrief,
                    client: '1',
                    is_goods: '11',
                    project_goods: '1',
                    class_id: '1',
                    image: this.data.goodsMainImg.major_img_big,
                    image_middle: this.data.goodsMainImg.major_img_middle,
                    image_thumb: this.data.goodsMainImg.major_img_thumb,
                    viceImages: JSON.stringify(this.data.goodsSubImg),
                    infoImages: JSON.stringify(this.data.goodsDetailImg),
                    sku_list: JSON.stringify(this.data.goodsFormat),
                    sort: this.data.goodsSort,
                    need_project_money: this.data.goodsPointChange,
                    is_sale: this.data.onSale ? '1' : '0'
                }
            }
            common.post('Goods/add_goods', data).then(function(res) {
                wx.hideLoading()
                that.setData({
                    isLoading: false
                })
                if (res.code == 204) {
                    wx.showModal({
                        title: '提示',
                        content: res.error,
                        showCancel: false,
                        success(res) {}
                    })
                    return
                }
                wx.showModal({
                    title: '提示',
                    content: '新增商品成功',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            wx.navigateBack({
                                delta: 1
                            })
                        }
                    }
                })
            }).catch(function(e) {})
        } else {
            if (this.data.goodsType == 'normal') {
                data = {
                    store_id: '1',
                    goods_id: this.data.goodsId,
                    goods_name: this.data.goodsName,
                    brief: this.data.goodsBrief,
                    client: '1',
                    is_goods: '0',
                    class_id: '1',
                    image: this.data.goodsMainImg.major_img_big,
                    image_middle: this.data.goodsMainImg.major_img_middle,
                    image_thumb: this.data.goodsMainImg.major_img_thumb,
                    sku_list: JSON.stringify(this.data.goodsFormat),
                    sort: this.data.goodsSort,
                    project_money: this.data.goodsPoint,
                    is_sale: this.data.onSale ? '1' : '0'
                }
            }
            else if (this.data.goodsType == 'new') {
                data = {
                    store_id: '1',
                    goods_id: this.data.goodsId,
                    goods_name: this.data.goodsName,
                    brief: this.data.goodsBrief,
                    client: '1',
                    is_goods: '11',
                    new_goods: '1',
                    class_id: '1',
                    image: this.data.goodsMainImg.major_img_big,
                    image_middle: this.data.goodsMainImg.major_img_middle,
                    image_thumb: this.data.goodsMainImg.major_img_thumb,
                    viceImages: JSON.stringify(this.data.goodsSubImg),
                    infoImages: JSON.stringify(this.data.goodsDetailImg),
                    sku_list: JSON.stringify(this.data.goodsFormat),
                    sort: this.data.goodsSort,
                    is_sale: this.data.onSale ? '1' : '0'
                }
            }
            else if (this.data.goodsType == 'share') {
                data = {
                    store_id: '1',
                    goods_id: this.data.goodsId,
                    goods_name: this.data.goodsName,
                    brief: this.data.goodsBrief,
                    client: '1',
                    is_goods: '11',
                    share_goods: '1',
                    class_id: '1',
                    image: this.data.goodsMainImg.major_img_big,
                    image_middle: this.data.goodsMainImg.major_img_middle,
                    image_thumb: this.data.goodsMainImg.major_img_thumb,
                    viceImages: JSON.stringify(this.data.goodsSubImg),
                    infoImages: JSON.stringify(this.data.goodsDetailImg),
                    sku_list: JSON.stringify(this.data.goodsFormat),
                    sort: this.data.goodsSort,
                    share_mun: this.data.goodsShare,
                    is_sale: this.data.onSale ? '1' : '0'
                }
            }
            else if (this.data.goodsType == 'point') {
                data = {
                    store_id: wx.getStorageSync('storeId'),
                    goods_id: this.data.goodsId,
                    goods_name: this.data.goodsName,
                    brief: this.data.goodsBrief,
                    client: '1',
                    is_goods: '11',
                    project_goods: '1',
                    class_id: '1',
                    image: this.data.goodsMainImg.major_img_big,
                    image_middle: this.data.goodsMainImg.major_img_middle,
                    image_thumb: this.data.goodsMainImg.major_img_thumb,
                    viceImages: JSON.stringify(this.data.goodsSubImg),
                    infoImages: JSON.stringify(this.data.goodsDetailImg),
                    sku_list: JSON.stringify(this.data.goodsFormat),
                    sort: this.data.goodsSort,
                    need_project_money: this.data.goodsPointChange,
                    is_sale: this.data.onSale ? '1' : '0'
                }
            }
            common.post('Goods/up_goods', data).then(function(res) {
                wx.hideLoading()
                that.setData({
                    isLoading: false
                })
                if (res.code == 204) {
                    wx.showModal({
                        title: '提示',
                        content: res.error,
                        showCancel: false,
                        success(res) {}
                    })
                    return
                }
                wx.showModal({
                    title: '提示',
                    content: '保存商品成功',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            wx.navigateBack({
                                delta: 1
                            })
                        }
                    }
                })
            }).catch(function(e) {})
        }
    },

    uploadBannerImg: function() {
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                const tempFilePaths = res.tempFilePaths[0];
                wx.navigateTo({
                    url: '/pages/cropper/cropper?type=4&target=goodsBannerImg&src=' + tempFilePaths + '&ratio=true&width=230&height=136'
                })
            }
        })
    },

    uploadImg: function(e) {
        const that = this
        let data = {}
        let key = wx.getStorageSync('token')
        let sign = 'jlzj'
        let timestamp = Date.parse(new Date())
        data.key = key
        data.timestamp = timestamp
        data = this.objKeySort(data)
        for (let i in data) {
            sign += (i + data[i])
        }
        sign += 'jlzj'
        sign = md5(sign)
        data.sign = sign
        wx.chooseImage({
            count: e.currentTarget.dataset.type == 'mainImg' || e.currentTarget.dataset.type == 'formatImg' ? 1 : 9, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                wx.showLoading({
                    title: '正在上传图片',
                })
                let tempFilePaths = res.tempFilePaths
                if (e.currentTarget.dataset.type == 'mainImg') {
                    wx.uploadFile({
                        url: 'https://s1.bailingyoupin.com/api/Upimg/up_major_img', //仅为示例，非真实的接口地址
                        filePath: tempFilePaths[0],
                        name: 'file',
                        formData: {
                            key: data.key,
                            timestamp: data.timestamp,
                            sign: data.sign
                        },
                        success(res) {
                            wx.hideLoading()
                            if (JSON.parse(res.data).code == 204) {
                                wx.showModal({
                                    title: '提示',
                                    content: JSON.parse(res.data).error,
                                    showCancel: false
                                })
                                return
                            }
                            wx.showToast({
                                title: '上传图片成功',
                                icon: 'none',
                                duration: 2000
                            })
                            that.setData({
                                goodsMainImg: JSON.parse(res.data).datas
                            })
                        }
                    })
                } else if (e.currentTarget.dataset.type == 'formatImg') {
                    wx.uploadFile({
                        url: 'https://s1.bailingyoupin.com/api/Upimg/up_sku_img', //仅为示例，非真实的接口地址
                        filePath: tempFilePaths[0],
                        name: 'file',
                        formData: {
                            key: data.key,
                            timestamp: data.timestamp,
                            sign: data.sign
                        },
                        success(res) {
                            wx.hideLoading()
                            if (JSON.parse(res.data).code == 204) {
                                wx.showModal({
                                    title: '提示',
                                    content: JSON.parse(res.data).error,
                                    showCancel: false
                                })
                                return
                            }
                            wx.showToast({
                                title: '上传图片成功',
                                icon: 'none',
                                duration: 2000
                            })
                            let goodsFormat = that.data.goodsFormat
                            goodsFormat[e.currentTarget.dataset.index].img = JSON.parse(res.data).datas
                            that.setData({
                                goodsFormat: goodsFormat
                            })
                        }
                    })
                } else if (e.currentTarget.dataset.type == 'subImg') {
                    if (that.data.goodsId == null) {
                        let goodsSubImg = that.data.goodsSubImg
                        let tempUrl = 'https://s1.bailingyoupin.com/api/Upimg/up_vice_img'
                        let tempData = {
                            key: data.key,
                            timestamp: data.timestamp,
                            sign: data.sign
                        }
                        common.uploadImage(tempFilePaths, tempUrl, tempData).then(function(res) {
                            res.forEach((e) => {
                                goodsSubImg.push({
                                    imgUrl: e
                                })
                                that.setData({
                                    goodsSubImg: goodsSubImg
                                })
                            })
                        })
                    } else {
                        let goodsSubImg = that.data.goodsSubImg
                        let tempUrl = 'https://s1.bailingyoupin.com/api/Goods/add_goods_img'
                        let tempData = {
                            goods_id: that.data.goodsId,
                            type: 4,
                            key: data.key,
                            timestamp: data.timestamp,
                            sign: data.sign
                        }
                        common.uploadImage(tempFilePaths, tempUrl, tempData).then(function(res) {
                            res.forEach((e) => {
                                goodsSubImg.push({
                                    image_id: e.image_id,
                                    imgUrl: e.imgUrl
                                })
                                that.setData({
                                    goodsSubImg: goodsSubImg
                                })
                            })
                        })
                    }
                } else if (e.currentTarget.dataset.type == 'detailImg') {
                    if (that.data.goodsId == null) {
                        let goodsDetailImg = that.data.goodsDetailImg
                        let tempUrl = 'https://s1.bailingyoupin.com/api/Upimg/up_info_img'
                        let tempData = {
                            key: data.key,
                            timestamp: data.timestamp,
                            sign: data.sign
                        }
                        common.uploadImage(tempFilePaths, tempUrl, tempData).then(function(res) {
                            res.forEach((e) => {
                                goodsDetailImg.push({
                                    imgUrl: e
                                })
                                that.setData({
                                    goodsDetailImg: goodsDetailImg
                                })
                            })
                        })
                    } else {
                        let goodsDetailImg = that.data.goodsDetailImg
                        let tempUrl = 'https://s1.bailingyoupin.com/api/Goods/add_goods_img'
                        let tempData = {
                            goods_id: that.data.goodsId,
                            type: 5,
                            key: data.key,
                            timestamp: data.timestamp,
                            sign: data.sign
                        }
                        common.uploadImage(tempFilePaths, tempUrl, tempData).then(function(res) {
                            res.forEach((e) => {
                                goodsDetailImg.push({
                                    image_id: e.image_id,
                                    imgUrl: e.imgUrl
                                })
                                that.setData({
                                    goodsDetailImg: goodsDetailImg
                                })
                            })
                        })
                    }
                }
            }
        })
    },

    objKeySort: function(obj) {
        let newkey = Object.keys(obj).sort()
        let newObj = {}
        for (let i = 0; i < newkey.length; i++) {
            newObj[newkey[i]] = obj[newkey[i]]
        }
        return newObj
    }
})